*******************************************************************************
*** BUILD DATA FOR SUMMARY STATS FOR PATIENTS WITH INPATIENT STAYS IN 2010	***
*** Last edited: 3/8/2022                       							***
*******************************************************************************

capture log close
log using "${CodePath}/log/build_medpar_summarystats_2010_$S_DATE.log", text replace


*--------------------------------------------------
* Build dataset of 2010 patients and their characteristics
*--------------------------------------------------
use bene_id race bene_dob sex provider file_year thru_dt from_dt using "${DataPath}/claims/ip100pct_2010_full.dta", clear
preserve
	use bene_id race bene_dob sex provider thru_dt from_dt using "${DataPath}/claims/ip100pct_2009_full.dta", clear
	gen file_year = 2009
	tempfile temp
	save `temp'
restore

append using `temp'
*keep if file_year == 2010 | file_year == 2009

// construct whether beneficiary had come in in the last 30, 60, or 90 days
bys bene_id (thru_dt): gen dayssincelast   = thru_dt[_n] - from_dt[_n-1]
gen sinceIP_30 = dayssincelast <= 30
gen sinceIP_60 = dayssincelast <= 60
gen sinceIP_90 = dayssincelast <= 90

keep if file_year == 2010


gen female = sex == "2"
gen white  = race == "1"
gen age_disch = (thru_dt - bene_dob)/365



gen nonnum = real(provider) == .
tab nonnum
drop if nonnum
destring provider, gen(pn)

// merge in within-100 mile of border group
preserve
	use  "${DataPath}/within100m/pnlist_groups_100m.dta", clear
	duplicates drop pn, force

	tempfile temp
	save `temp'
restore
merge m:1 pn using `temp', keep(1 3) keepusing(pn) gen(within100m)

replace within100m = 0 if within100m!=3
replace within100m = 1 if within100m == 3

save "${DataPath}/claims/ip100pct_withccs_2010.dta", replace


log close